/*
 *  MenuState.cpp
 *  Example "menu" state
 *
 *  Created by Marcelo Cohen on 04/11.
 *  Copyright 2011 PUCRS. All rights reserved.
 *
 */

#include <iostream>
#include <SDL.h>
#include "Graphics.h"
#include "CGame.h"
#include "MenuState.h"
#include "PlayStage1.h"

MenuState MenuState::m_MenuState;

using namespace std;

void MenuState::init()
{
    // Titulo do jogo
    menuTitle = new CImage();
    menuTitle->loadImage("data/img/menu-title.png");
    menuTitle->setPosition(200, 75);

    // Itens do menu
    menuItens[0] = new CSprite();
    menuItens[1] = new CSprite();
    menuItens[2] = new CSprite();
    menuItens[0]->loadSpriteSparrowXML("data/img/menu-new.xml");
    menuItens[1]->loadSpriteSparrowXML("data/img/menu-options.xml");
    menuItens[2]->loadSpriteSparrowXML("data/img/menu-quit.xml");

    // Posicao inicial de cada item do menu
    menuItens[0]->setPosition(250, 200);
    menuItens[1]->setPosition(250, 270);
    menuItens[2]->setPosition(250, 370);

    // activeFrames armazena o no. do frame ativo para
    // cada opcao do menu (e' atualizado no evento de teclado)
    activeFrames[0] = 0;
    activeFrames[1] = 1;
    activeFrames[2] = 1;
    current = 0; // new game

    firstTime = true;

	cout << "MenuState Init Successful" << endl;
}

void MenuState::cleanup()
{
    delete menuTitle;
    delete menuItens[0];
    delete menuItens[1];
    delete menuItens[2];
	cout << "MenuState Cleanup Successful" << endl;
}

void MenuState::pause()
{
	cout << "MenuState Paused" << endl;
}

void MenuState::resume()
{
	cout << "MenuState Resumed" << endl;
}

void MenuState::handleEvents(CGame* game)
{
	SDL_Event event;

    if (SDL_PollEvent(&event)) {
		switch (event.type) {
			case SDL_QUIT:
				game->quit();
				break;

			case SDL_KEYDOWN:
                switch(event.key.keysym.sym) {

                    case SDLK_DOWN:
                        // desativa o item atual
                        activeFrames[current] = 1;
                        current++;
                        if(current > 2) current = 0;
                        // ativa o proximo item
                        activeFrames[current] = 0;

                        // ativa o som
                        audio->play2D(changeSound);

                        break;
                    case SDLK_UP:
                        // desativa o item atual
                        activeFrames[current] = 1;
                        current--;
                        if(current < 0) current = 2;
                        // ativa o proximo item
                        activeFrames[current] = 0;

                        // ativa o som
                        audio->play2D(changeSound);

                        break;
                    case SDLK_RETURN:
                        cout << "Opcao selecionada: " << current << endl;
                        if(current == 0)
                            game->changeState(PlayStage1::instance());
                        else if(current == 1)
                            cout << "Options" << endl;
                        else // quit
                            game->quit();
                        break;
                    case SDLK_ESCAPE:
                        game->quit();
                        break;
                    default:
                        break;
                }
		}
	}
}

void MenuState::update(CGame* game)
{
    if(firstTime) {
        audio = game->getAudioEngine();
        changeSound = audio->addSoundSourceFromFile("data/audio/menu-change.wav");
        firstTime = false;
    }
}

void MenuState::draw(CGame* game)
{
    glClearColor(0, 0, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT);
    menuTitle->draw();

    for(int i = 0; i < 3; i++)
        menuItens[i]->drawFrame(activeFrames[i]);

    SDL_GL_SwapBuffers();
}

